<?php

namespace app\index\controller;

use app\model\Member;
use app\model\UsersSessions;
use app\model\Openid;
use app\model\Technical;
use app\model\Store;

class Base extends \app\BaseController
{
    protected $userInfo = [];

    protected function initialize()
    {
        $this->checkTokenAuth();
    }
    //取uuid
    protected function getuuid()
    {
        $uuid = $this->userInfo['uuid'];

        if (empty($uuid)) {
            $uuid = Openid::getuuid();
            $this->userInfo['uuid'] = $uuid;
            $this->setToken($this->userInfo, $this->getToken());
            if (!empty($uuid)) {
                $Membermob = new Member;
                $memberinfo = $Membermob->getUserByWechat();
                if (!empty($memberinfo) && empty($memberinfo['uuid'])) {
                    Member::where('id', $memberinfo['id'])->update(['uuid' => $uuid]);
                }
            }
        }

        if (empty($uuid)) {
            $Membermob = new Member;
            $memberinfo = $Membermob->getUserByWechat();
            $uuid = $memberinfo['uuid'];

            if (!empty($memberinfo) && empty($uuid)) {

                $Technical = Technical::where('uid', $memberinfo['id'])->find();
                $uuid = $Technical->uuid;

                if (empty($uuid)) {
                    $Store =  Store::where('uid', $memberinfo['id'])->find();
                    $uuid = $Store->uuid;
                }

                if (empty($uuid)) {
                    $uuid = uniqid(rand(1, 10000));
                    Member::where('id', $memberinfo['id'])->update(['uuid' => $uuid]);
                }
                if (Technical::where('uid', $memberinfo['id'])->find() && empty($Technical->uuid)) {
                    Technical::where('uid', $memberinfo['id'])->update(['uuid' => $uuid]);
                }
                if (Store::where('uid', $memberinfo['id'])->find() && empty($Store->uuid)) {
                    Store::where('uid', $memberinfo['id'])->update(['uuid' => $uuid]);
                }
            }
            if (!empty($uuid) && !empty(getFans()['openid'])) {
                Openid::create(['weid' => weid(), 'uuid' => $uuid, 'ptype' => getFans()['ptype'], 'openid' => getFans()['openid']]);
            }
        }

        return $uuid;
    }

    //设置token
    protected function setToken($data, $token = '')
    {
        $weid = $data['weid'];

        if (empty($token)) {
            $token = md5(uniqid());
        }

        if (empty($data['ptype'])) {
            $data['ptype'] = 'wxapp';
        }

        //登录的时候把token写入数据表
        $tokenInfo = UsersSessions::where('token', $token)->find();
        $data = serialize($data);
        if (empty($tokenInfo)) {
            UsersSessions::create([
                'weid' => $weid,
                'token' => $token,
                'ptype' => $data['ptype'],
                'ip' => getRealIP(),
                'expire_time' => time(),
                'data' => $data,
                'status' => 1
            ]);
        } else {
            UsersSessions::where('token', $token)->update([
                'weid' => $weid,
                'token' => $token,
                'ptype' => $data['ptype'],
                'ip' => getRealIP(),
                'expire_time' => time(),
                'data' => $data,
                'status' => 1
            ]);
        }

        return $token;
    }
    //取token
    protected function getToken()
    {

        $state = input('param.xmtoken');

        if (empty($state)) {
            $state = $this->request->header('Authorization');
        }

        if (empty($state)) {
            $state = input('get.state');
        }

        if (empty($state)) {
            $state = input('param.state');
        }

        $token = str_replace("we7sid-", "", $state);
        return $token;
    }

    //检测token
    protected function checkTokenAuth()
    {
        global $_W;
        $token = $this->getToken();
        $tokenInfo = UsersSessions::where('token', $token)->find();
        if (!empty($tokenInfo)) {
            $tokenInfo = $tokenInfo->toArray();
            $this->userInfo = iunserializer($tokenInfo['data']);

            if (empty($this->userInfo['nickname']) || empty($this->userInfo['avatar'])) {
                if (!empty($this->userInfo['openid'])) {
                    $uid =  \app\model\Openid::getuidbyopenid($this->userInfo['openid']);
                    if (!empty($uid)) {
                        $member = \app\model\Member::find($uid);
                        if (!empty($member)) {
                            $this->userInfo['nickname'] = $member->nickname;
                            $this->userInfo['avatar'] = $member->userpic;
                        }
                    }
                }
            }
            if (empty($_W['uniacid']) && !empty($tokenInfo['weid'])) {
                $_W['uniacid'] = $tokenInfo['weid'];
            }

            if (empty($_W['fans']['openid']) && !empty($this->userInfo['openid'])) {
                $_W['fans']['openid'] = $this->userInfo['openid'];
            }
            $_W['fans']['ptype'] = $this->userInfo['ptype'];
            event('DoLog', $this->userInfo['username']);    //写入操作日志
        } else {
            if (!empty($_W['fans']['openid'])) {
                $data['openid'] = $_W['fans']['openid'];
                $data['ptype'] = $_W['fans']['ptype'];
                $data['weid'] = weid();
                $this->setToken($data, $token);
            }
        }
    }
}
